#!/bin/bash
ver=1.5

#global vars initialization (def values)
#get the file basename
progname=$(basename $0)
filetype='-name "*.txt"'
mark=0		#voom mark button
unix=0		#unix to dos button
filename=1	#button of file type merge
interactive=
verbose=
dir=.
file=all_test.txt
tarball=
ft_tgz=" -or -name \"*.tar.gz\""
ft_zip=" -or -name \"*.zip\""
ft_rar=" -or -name \"*.rar\""

echo -e "$progname version $ver\n,author ping,copyright 2010-2012"

#usage prompt
usage () {
    echo "$progname: usage: $progname [options] DIR FILENAME"
    cat <<- _USAGE_
	[options]
	    -t (txt|htm|log|...):type of file you want to merge
	    -m:	add marks on the matched file names when marge (for vim voom)
	    -M: don't append marks
	    -u:	convert to unix
	    -U: don't convert to unix
	    -b: add extra empty lines between each files
	    -i: interactive mode (not done yet)
	    -T:	tarball support (.tar.gz,new!)
_USAGE_
    return
}

#options handling
while getopts ":t:mMivuUTh" opt; do
    case $opt in
	t ) 	filetype="-name \"*$OPTARG\""
		echo -e "get an option: -t $OPTARG"
		echo -e "compose filetype to be:$filetype\n"
		;;
	T )	tarball=1
		echo -e "tarball option enabled"
		;;
	m )	mark=1
		echo -e "force marks"
		;;	
	M )	mark=0
		echo -e "no marks"
		;;
	u )	unix=1
		echo -e "force unix"
		;;
	U )	unix=0
		echo -e "no dos-unix conversion"
		;;
	i )     interactive=1
		#todo
              	;;
	b )	blanks=1
		;;
	v )	verbose=1
		#todo
		;;
	h )     usage
		exit
		;;
	\?) 	usage 1>&2
		exit 1
		;;
    esac
done

#handling other parameters following options
shift $(($OPTIND - 1))

#if [[ -z $filetype || -z $dir || -z $file ]]; then
#    usage >&2
#    exit 1
#fi
	    
#if no dir and filename followed,exit
if [[ -z "$@" ]]; then
    usage 1>&2
    exit 1
fi

#dir & filename parsing
dir=$1
file=$2
echo "---dir $dir will be checked, and"
echo "---all files found will be merged into $file"

#rm the old file generated last time
if [ -e $file ]; then
    echo -e "---remove the old file:$file"
    rm $file
fi

format () {
	if [ $unix -eq 1 ]; then
	{
	#convert from dos to unix
	echo -e "convert to unix"
	cat "$afile" | tr -d '\r' > temp.txt 
	mv temp.txt "$afile"
	}
	fi

	if [ $mark -eq 1 ]; then
	{
	    #mark for Voom
	    echo -e "\n\n$afile {{{1\n\n" >> $file
	    echo "append Voom marks"
	}
	fi
}


findamerge () {
    dir=$1 
    filetype=$2
    echo "---dir got is $dir"
    echo "---filetype got is $filetype"
    tofind="find \"$dir\" -type f $filetype"
    echo -e "---the following find operation will be executed to find the desired files: $tofind\n"

    eval $tofind  | xargs -0 ls  | 
    while read afile;do
	echo "get a file:$afile"
	#file type handling
	case $afile in
	    *.htm?)	
	       #if it's html
	       format
	       echo -e "render the webpage"
	       #render to text by w3m 
	       cat "afile" | w3m -T text/html -dump >> $file ;;

	    *txt|*.log|*.cfg)
	       #otherwise,just cat it 
	       format
	       echo -e "cat the plain txt files"
	       cat "$afile" >> $file 
	       echo -e "\n======================\n" >> $file
	       ;;

	    *rar)
		#todo
	      ;; 

	    *zip)
		#todo
	      ;;

	    *.tar.gz)
		#for tar.gz ball,1st to check out files per file type, 
		#if there is match then extract them out and merge,recursively
		if [ $tarball -eq 1 ]; then
		{
		    n=`IFS=$'\n';tar tf $afile | grep txt | wc -l`
		    if [[ $n -gt 0  ]]; then
		    { #if match found,handle them
			tmpdir4targz=$(basename $afile)
			echo -e "tmp dir name is $tmpdir4targz after basename"
			tmpdir4targz=${tmpdir4targz%.*.*}
			echo -e "tmp dir name is $tmpdir4targz after trunc"
			tmpdir4targz="/tmp/logtool.tmp/$tmpdir4targz/"

			#create a temp dir
			if [ ! -e $tmpdir4targz ]; then
			{
			     echo -e "temp dir $tmpdir4targz does not exist, create one"
			     mkdir -p $tmpdir4targz
			}
			else
			{
			    echo -e "temp dir $tmpdir4targz existed!"
			}
			fi

			#and untar everything
			tar zxf $afile -C $tmpdir4targz/ 
			findamerge "$tmpdir4targz" "$filetype"
		    }
		    else
		    {
			echo -e "no matching files found inside this tarball!"
		    }
		    fi
		}
		fi
		;;

	    *)
	       echo -e "don't do anything for other types\n"
	       ;;
	esac
    done

}

filetype="$filetype -print0 $ft_tgz -print0"
findamerge "$dir" "$filetype" 
